# ============================================
# Mall 微服务项目 - 订单流程测试脚本 (PowerShell)
# ============================================

Write-Host "==========================================" -ForegroundColor Cyan
Write-Host "Mall 微服务订单流程测试" -ForegroundColor Cyan
Write-Host "==========================================" -ForegroundColor Cyan
Write-Host ""

# 1. 检查服务状态
Write-Host "1. 检查服务状态..." -ForegroundColor Yellow
Write-Host ""

# 检查订单服务
Write-Host "检查订单服务 (8081)... " -NoNewline
try {
    $null = Invoke-RestMethod -Uri "http://localhost:8081/" -Method Get -TimeoutSec 2 -ErrorAction Stop
    Write-Host "[OK] 运行中" -ForegroundColor Green
} catch {
    Write-Host "[ERROR] 未启动" -ForegroundColor Red
    Write-Host "请先启动 order-service" -ForegroundColor Red
    exit 1
}

# 检查库存服务
Write-Host "检查库存服务 (8082)... " -NoNewline
$port8082 = netstat -ano | findstr ":8082" | findstr "LISTENING"
if ($port8082) {
    Write-Host "[OK] 运行中" -ForegroundColor Green
} else {
    Write-Host "[ERROR] 未启动" -ForegroundColor Red
    Write-Host "请先启动 inventory-service" -ForegroundColor Red
    exit 1
}

# 检查支付服务
Write-Host "检查支付服务 (8083)... " -NoNewline
$port8083 = netstat -ano | findstr ":8083" | findstr "LISTENING"
if ($port8083) {
    Write-Host "[OK] 运行中" -ForegroundColor Green
} else {
    Write-Host "[ERROR] 未启动" -ForegroundColor Red
    Write-Host "请先启动 payment-service" -ForegroundColor Red
    exit 1
}

Write-Host ""

# 2. 创建测试订单
Write-Host "2. 创建测试订单..." -ForegroundColor Yellow
Write-Host ""

$body = @{
    userId = 1001
    shippingAddress = "北京市朝阳区xxx街道xxx号"
    remark = "测试订单 - 请尽快发货"
    items = @(
        @{
            skuId = 1
            skuName = "iPhone 15 Pro 256GB 深空黑色"
            price = "7999.00"
            quantity = 1
        },
        @{
            skuId = 3
            skuName = "AirPods Pro 2 主动降噪"
            price = "1899.00"
            quantity = 2
        }
    )
} | ConvertTo-Json -Depth 10

try {
    $response = Invoke-RestMethod -Uri "http://localhost:8081/api/orders" -Method Post -Body $body -ContentType "application/json"
    Write-Host "[OK] 订单创建成功" -ForegroundColor Green
    Write-Host "订单编号: $($response.orderNo)" -ForegroundColor Cyan
    Write-Host ""
    
    # 显示订单详情
    Write-Host "订单详情:" -ForegroundColor Yellow
    $response | ConvertTo-Json -Depth 10 | Write-Host
    Write-Host ""
    
    $orderNo = $response.orderNo
} catch {
    Write-Host "[ERROR] 订单创建失败" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
    exit 1
}

# 3. 等待订单处理
Write-Host "3. 等待订单处理..." -ForegroundColor Yellow
Write-Host ""
Write-Host "订单处理流程:" -ForegroundColor Cyan
Write-Host "  1. order-service 创建订单 → 发送 order_created 事件"
Write-Host "  2. inventory-service 预留库存 → 发送 order_stock_reserved 事件"
Write-Host "  3. payment-service 模拟支付 → 发送 order_paid 事件"
Write-Host "  4. order-service 更新订单状态为已支付"
Write-Host ""

# 等待订单处理完成（最多等待 10 秒）
$maxWait = 10
$waitCount = 0

Write-Host "等待中" -NoNewline
while ($waitCount -lt $maxWait) {
    Start-Sleep -Seconds 1
    $waitCount++
    Write-Host "." -NoNewline
    
    try {
        $orderStatus = Invoke-RestMethod -Uri "http://localhost:8081/api/orders/$orderNo" -Method Get
        if ($orderStatus.status -eq "PAID") {
            Write-Host ""
            Write-Host "[OK] 订单处理完成！" -ForegroundColor Green
            break
        }
    } catch {
        # 继续等待
    }
}

Write-Host ""
Write-Host ""

# 4. 查询订单最终状态
Write-Host "4. 查询订单最终状态..." -ForegroundColor Yellow
Write-Host ""

try {
    $finalOrder = Invoke-RestMethod -Uri "http://localhost:8081/api/orders/$orderNo" -Method Get
    
    Write-Host "订单编号: $($finalOrder.orderNo)" -ForegroundColor Cyan
    Write-Host "订单状态: $($finalOrder.status)" -ForegroundColor Cyan
    Write-Host ""
    
    if ($finalOrder.status -eq "PAID") {
        Write-Host "==========================================" -ForegroundColor Green
        Write-Host "✓ 测试成功！订单已完成支付" -ForegroundColor Green
        Write-Host "==========================================" -ForegroundColor Green
    } else {
        Write-Host "==========================================" -ForegroundColor Yellow
        Write-Host "订单状态: $($finalOrder.status)" -ForegroundColor Yellow
        Write-Host "可能还在处理中，请稍后查询" -ForegroundColor Yellow
        Write-Host "==========================================" -ForegroundColor Yellow
    }
    
    Write-Host ""
    Write-Host "完整订单信息:" -ForegroundColor Yellow
    $finalOrder | ConvertTo-Json -Depth 10 | Write-Host
    Write-Host ""
    
} catch {
    Write-Host "[ERROR] 查询订单失败" -ForegroundColor Red
    Write-Host $_.Exception.Message -ForegroundColor Red
}

# 5. 提示
Write-Host "提示:" -ForegroundColor Yellow
Write-Host "- 可以查看各服务的日志了解详细处理过程"
Write-Host "- 可以访问 AKHQ (http://localhost:9001) 查看 Kafka 消息"
Write-Host "- 可以查询数据库验证数据变化"
Write-Host ""

Write-Host "查询数据库命令:" -ForegroundColor Cyan
Write-Host "docker exec -it thunderbit-mysql mysql -u thunderbit -pthunderbit123 order_db -e `"SELECT * FROM t_order WHERE order_no = '$orderNo';`""
Write-Host ""

